class EnferiController extends Bot;

var EnferiPathNode pn;
var array<EnferiPathNode> PatrolPath;
var PathNodeInfo pni;
var bool bPathsEstablished;
var int currentPathNode;
var int totalPathNodes;

var bool bMovingForward;

function PostBeginPlay()
{
  local int i;

  super.PostBeginPlay();

  foreach AllActors(class'EnferiPathNode', pn) {
    if (pn != none && EnferiPawn(Pawn) != none && pn.MyPatrolPath == EnferiPawn(Pawn).MyPatrolPath) {
      i++;
      //Level.GetLocalPlayerController().ClientMessage("Looking for nodes "@pn.Name);
    }
  }
  PatrolPath.length = i;
}

function SetAttractionState()
{
  if ( Enemy != None )
    GotoState('Hunting');
  else
    GotoState('Patrolling');
}

/*
function bool IsVisibleToMe(Pawn ThisPawn)
{
      local float Distance;         //Distance between my pawn and pawn in question
      local float VisFactor;        //converted visibility of enemy in float ranging from 0-2

      If (!CanSee(ThisPawn))        //view blocked or looking in other direction
             return false;

      Distance = VSize(ThisPawn.Location - Pawn.Location);
      VisFactor = ThisPawn.Visibility / 128;

      If ( (Distance/VisFactor) <= Pawn.SightRadius) //can see enemy
             return true;
      return false;
}
*/
state Patrolling
{
  function BeginState()
  {
    local int i;
    Enemy = none;

    i = 0;
    foreach AllActors(class'EnferiPathNode', pn) {
      if (pn != none && EnferiPawn(Pawn) != none && pn.MyPatrolPath == EnferiPawn(Pawn).MyPatrolPath) {
        PatrolPath[i] = pn;
        i++;
        //Level.GetLocalPlayerController().ClientMessage("Looking for nodes "@pn.Name);
      }
    }
    totalPathNodes = i - 1;

  //  log("In patrolling state");
  }

  function CheckForEnemy()
  {
    local EnyaraPawn ep;
    local float dist;

    foreach AllActors(class'EnyaraPawn', ep) {
      if (Pawn != none) {
        dist = VSize(ep.Location - pawn.Location);
        if (LineOfSightTo(ep) && dist < 1500) {
         // Level.GetLocalPlayerController().ClientMessage("I see you!!");
          Enemy = ep;
          Target = Enemy;
          Focus = Target;
          GotoState('Hunting');
        }
      }
    }
  }

Begin:
  //Level.GetLocalPlayerController().ClientMessage("In Begin", 'Critical');
//  goto('KeepMoving');

KeepMoving:
  CheckForEnemy();
  FindPathToward(PatrolPath[CurrentPathNode]);
  if (MoveTarget != none) {
    if (PatrolPath[CurrentPathNode] != none) {
      if ( Pawn != none && !Pawn.ReachedDestination(PatrolPath[CurrentPathNode])) {
        MoveToward(FindPathToward(PatrolPath[CurrentPathNode]),PatrolPath[CurrentPathNode],,,true);
        //Level.GetLocalPlayerController().ClientMessage("Moving to"@PatrolPath[CurrentPathNode].Name);
        Goto('KeepMoving');
      } else {
        if (bMovingForward) {
            if (CurrentPathNode < totalPathNodes - 1)
              CurrentPathNode++;
            else
              bMovingForward = false;
        } else {
          if (currentPathNode > 0)
            currentPathNode--;
          else
            bMovingForward = true;
        }

        if (PatrolPath[CurrentPathNode].bShouldPause) {
           if (PatrolPath[CurrentPathNode].pauseLength > 0)
              sleep(PatrolPath[CurrentPathNode].pauseLength);
           else
              sleep(2);
        }

        //Level.GetLocalPlayerController().ClientMessage("Reached Destination: "@PatrolPath[CurrentPathNode].Name);
        Goto('KeepMoving');
      }
    }
  }
}

defaultproperties
{
     PawnClass=Class'Enyara.EnferiPawn'
}
